14. Exercise: Arrays

Exercise: Arrays

Let's do a few simple exercises to make sure the syntax for creating and accessing arrays is clear! If you get stuck, remember that all the syntax is shown on the previous page—and there is solution code given below as well.

Lift off!

Task Description:

In the workspace below, your goal is to use only arrays and println to get the following output in the terminal:

Ignition sequence start!
4
3
2
1
Liftoff!

Here are the steps:

Task List:

Task Feedback:

Nice work! 🚀

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    export PATH=/data/jdk-15.0.1/bin:$PATH
    export JAVA_HOME=/data/jdk-15.0.1/bin

  • Solution: Liftoff!

    public class ArraysExercise{
    
        public static void main(String[] args){
            int [] numbers = {1, 2, 3, 4};
            String [] words  = {"Ignition sequence start!", "Liftoff!"};
    
            System.out.println(words[0]);
            System.out.println(numbers[4]);
            System.out.println(numbers[3]);
            System.out.println(numbers[2]);
            System.out.println(numbers[1]);
            System.out.println(numbers[0]);
            System.out.println(words[1]);
    
        }
    
    }

    Creating an Array with the new

    Task Description:

    At the end of the previous page, we also saw that there is an alternative way to create an array, using the new keyword. Go back up to the workspace and see if you can modify your code so that it uses the new keyword to create the numbers array.

    (Again, we will learn more about how this code works when we explore classes and objects later on!)

    Task List:

    Task Feedback:

    Nice work!

    Solution: Creating an Array with new

    ND079 C1 L1 A11 Arrays Exercise Solution

    The basic syntax for this approach is:

    // Create an array using the int data type
    int[] numbers;
    
    // Initialize the array to a size (in this case, five)
    numbers = new  int[5];
    
    //Add integers to each of the five locations in the array
    numbers[0] = 1;
    numbers[1] = 2;
    numbers[2] = 3;
    numbers[3] = 4;
    numbers[4] = 5;

    Note that we could also combine the first two lines (int[] numbers and numbers = new int[4];) like this:

    int[] numbers = new int[4];

    This creates the array and initializes its size at the same time. The end result is the same either way.

    Up Next: Loops!

    When we wrote our original code, we had to add a separate println statement for each number we printed in the numbers array—that is, something like this:

    System.out.println(numbers[4]);
    System.out.println(numbers[3]);
    System.out.println(numbers[2]);
    System.out.println(numbers[1]);
    System.out.println(numbers[0]);

    That is pretty tedious.

    In the video, you may have noticed that Jeff used a different approach:

    for(int i = 0; i < numbers.length; i++){
        System.out.println(numbers[i]);
    }

    This is a for loop. Using this block of code, we can print all the numbers in the array using a single println statement. We'll look at how to create loops like this next!